#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton*button=new QPushButton(this);
    button->setText("按钮");
    button->setGeometry(0,0,300,100);

    //为按钮设置图标
    //定义图片管理对象
    QIcon icon(":/image.jpg");
    //为按钮设置图标
    button->setIcon(icon);
    //为按钮设置图标大小
    button->setIconSize(QSize(80,80));

    //为按钮设置图标
    ui->pushButton_main->setIcon(QIcon(":/image.jpg"));
    ui->pushButton_up->setIcon(QIcon(":/image/image.jpg"));
    ui->pushButton_down->setIcon(QIcon(":/image/image.jpg"));
    ui->pushButton_left->setIcon(QIcon(":/image/image.jpg"));
    ui->pushButton_right->setIcon(QIcon(":/image/image.jpg"));
    //为按钮设置图标大小
    ui->pushButton_main->setIconSize(QSize(100,100));
    ui->pushButton_up->setIconSize(QSize(50,50));
    ui->pushButton_down->setIconSize(QSize(50,50));
    ui->pushButton_left->setIconSize(QSize(50,50));
    ui->pushButton_right->setIconSize(QSize(50,50));
    //为按钮设置槽函数
    //这里利用槽函数名称来匹配
    //为按钮设置快捷键
    //设置单一快捷键
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));
//    //设置组合快捷键
//    ui->pushButton_up->setShortcut(QKeySequence("ctrl+w"));
//    ui->pushButton_down->setShortcut(QKeySequence("ctrl+s"));
//    ui->pushButton_left->setShortcut(QKeySequence("ctrl+a"));
//    ui->pushButton_right->setShortcut(QKeySequence("ctrl+d"));

    //设置快捷键规范写法
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key::Key_D));

//    ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL+Qt::Key::Key_W));
//    ui->pushButton_down->setShortcut(QKeySequence(Qt::CTRL+Qt::Key::Key_S));
//    ui->pushButton_left->setShortcut(QKeySequence(Qt::CTRL+Qt::Key::Key_A));
//    ui->pushButton_right->setShortcut(QKeySequence(Qt::CTRL+Qt::Key::Key_D));

    //为按钮设置可重复触发
    //键盘是默认就可以重复触发的，这里主要是为点击一下即可连点设置的
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    //获取当前控件的位置尺寸信息
    QRect rect = ui->pushButton_main->geometry();
    //设置新的位置尺寸信息
    ui->pushButton_main->setGeometry(rect.x(),rect.y()-5,rect.width(),rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    //获取当前控件的位置尺寸信息
    QRect rect = ui->pushButton_main->geometry();
    //设置新的位置尺寸信息
    ui->pushButton_main->setGeometry(rect.x(),rect.y()+5,rect.width(),rect.height());
}
void Widget::on_pushButton_left_clicked()
{
    //获取当前控件的位置尺寸信息
    QRect rect = ui->pushButton_main->geometry();
    //设置新的位置尺寸信息
    ui->pushButton_main->setGeometry(rect.x()-5,rect.y(),rect.width(),rect.height());
}
void Widget::on_pushButton_right_clicked()
{
    //获取当前控件的位置尺寸信息
    QRect rect = ui->pushButton_main->geometry();
    //设置新的位置尺寸信息
    ui->pushButton_main->setGeometry(rect.x()+5,rect.y(),rect.width(),rect.height());
}
